package read;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ReadDemoByJDBC {
	
	private static final int MAX_PRINT_ROW_COUNT = 1000;
	
	public static void main(String args[]) throws ClassNotFoundException, SQLException{
		String host = "192.168.130.15";
		String port = "6667";
		String username = "root";
		String password = "root";
		String cmd = "select * from root.car";
		Class.forName("com.corp.tsfile.jdbc.TsfileDriver");
		Connection connection = null;
		connection = DriverManager.getConnection("jdbc:tsfile://" + host + ":" + port + "/x", username,
				password);
		Statement statement = connection.createStatement();
		statement.setFetchSize(500000);
		boolean hasResultSet = statement.execute(cmd);
		if (hasResultSet) {
			ResultSet resultSet = statement.getResultSet();
			output(resultSet, true);
		}
		statement.close();
		connection.close();
		System.out.println("Execute successfully.");
	}
	
	public static void output(ResultSet res, boolean printToConsole) {
		try {
			File file = new File("src/main/resources/output/queryRes.csv");
			if (!file.exists()) {
				if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				file.createNewFile();
			}
			int cnt = 0;
			int colCount = res.getMetaData().getColumnCount();
			// //Output Labels
			String format = "|%15s|";
			String blockLine = "";
			if (printToConsole) {

				int maxv = 15;
				for (int i = 0; i < colCount; i++) {
					int len = res.getMetaData().getColumnLabel(i).length();
					maxv = maxv < len ? len : maxv;
				}

				for (int i = 0; i < maxv; i++) {
					blockLine += "-";
				}
				System.out.printf("+" + blockLine + "+");
				for (int i = 0; i < colCount - 1; i++) {
					System.out.printf(blockLine + "+");
				}
				System.out.printf("\n");

				format = "%" + maxv + "s|";
				System.out.printf("|" + format, "Timestamp");
				for (int i = 0; i < colCount - 1; i++) {
					System.out.printf(format, res.getMetaData().getColumnLabel(i + 1));
				}
				System.out.printf("\n");

				System.out.printf("+" + blockLine + "+");
				for (int i = 0; i < colCount - 1; i++) {
					System.out.printf(blockLine + "+");
				}
				System.out.printf("\n");
			}

			// Output values
			while (res.next()) {
				StringBuilder line = new StringBuilder();
				line.append(String.valueOf(res.getString(0)));

				if (printToConsole && cnt < MAX_PRINT_ROW_COUNT) {
					System.out.printf("|" + format, String.valueOf(res.getString(0)));
				}

				for (int i = 1; i < colCount; i++) {
					line.append(",");
					line.append(res.getString(i));
					if (printToConsole && cnt < MAX_PRINT_ROW_COUNT) {
						System.out.printf(format, String.valueOf(res.getString(i)));
					}
				}

				if (printToConsole && cnt < MAX_PRINT_ROW_COUNT) {
					System.out.printf("\n");
				}

				cnt++;

				if (!printToConsole && cnt % 10000 == 0) {
					System.out.println(cnt);
				}
			}

			if (printToConsole) {
				System.out.printf("+" + blockLine + "+");
				for (int i = 0; i < colCount - 1; i++) {
					System.out.printf(blockLine + "+");
				}
				System.out.printf("\n");
			}
			System.out.println("Result size : " + cnt);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
